package com.dk.controller;

import com.dk.pojo.Role;
import com.dk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleService roleService;


    @RequestMapping("getInfo")
    @ResponseBody
    public List<Role> getInfo() {
        List<Role> info = roleService.getInfo();
        return info;
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "role/add";
    }

    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(Role role, @RequestParam(value = "nodeids", required = false) Integer[] nodeids) {
        int i = roleService.insertInfo(role, nodeids);
        return i > 0;
    }

    @RequestMapping("toUpdate/{rid}")
    public String toUpdate(@PathVariable("rid") Integer rid, Model model) {
        Role role = roleService.getInfoByRid(rid);
        model.addAttribute("role", role);
        return "role/update";
    }

    @RequestMapping("updateRole")
    @ResponseBody
    public boolean updateRole(Role role, @RequestParam("nodeids") Integer[] nodeids) {
        int i = roleService.updateRole(role, nodeids);
        return i > 0;
    }

    @RequestMapping("deleteRoleById/{rid}")
    @ResponseBody
    public boolean deleteRoleById(@PathVariable("rid") Integer rid) {
        int i = roleService.deleteRoleById(rid);
        return i > 0;
    }
}
